package com.powerunion.datacollection.workflow.fsm.core;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.powerunion.datacollection.workflow.EngineContext;

//import com.tw.framework.base.name.FName;
//import com.tw.framework.biz.base.wf.fsm.WfActivity;
//import com.tw.framework.biz.base.wf.fsm.WfProcess;
//import com.tw.framework.biz.base.wf.fsm.WorkItem;
//import com.tw.framework.openapi.biz.wf.EngineContext;


public class AbstractWfObject {

	private static Log log = LogFactory.getLog(AbstractWfObject.class);

	protected EngineContext wkctx;

	

	public void update(EngineContext wkctx) {

		int wkDBid = wkctx.getWkDBid();
		String type = wkctx.getEventType();
		String event = wkctx.getEvent();
		log.debug(".......wkDBid:[" + wkDBid + "],type:[" + type + "]:event["+ event + "]");

		if (type.equals(FName.wk_object_event_type_process)) {

			WfProcess proc = ProcessFactory.getProcess(wkctx);

			if (event.equals(FName.wk_process_event_to_notstart)) {
				proc.start();
			}
			if (event.equals(FName.wk_process_event_to_run)) {
				proc.run();
			}
			if (event.equals(FName.wk_process_event_to_complete)) {
				proc.complete();
			}
			if (event.equals(FName.wk_process_event_to_abort)) {
				proc.abort();
			}
			if (event.equals(FName.wk_process_event_to_cancel)) {
				proc.cancel();
			}
			if (event.equals(FName.wk_process_event_to_resume)) {
				proc.resume();
			}
			if (event.equals(FName.wk_process_event_to_suspend)) {
				proc.suspend();
			}
			if (event.equals(FName.wk_process_event_to_terminate)) {
				proc.terminate();
			}

		} else

		if (type.equals(FName.wk_object_event_type_activity)) {
		
			WfActivity act = ActivityFactory.getActivity(wkctx);

			if (event.equals(FName.wk_activity_event_to_notstart)) {
				act.start();
			}
			if (event.equals(FName.wk_activity_event_to_run)) {
				act.run();
			}
			if (event.equals(FName.wk_activity_event_to_complete)) {
				act.complete();
			}
			if (event.equals(FName.wk_activity_event_to_abort)) {
				act.abort();
			}
			if (event.equals(FName.wk_activity_event_to_resume)) {
				act.resume();
			}
			if (event.equals(FName.wk_activity_event_to_suspend)) {
				act.suspend();
			}
			if (event.equals(FName.wk_activity_event_to_terminate)) {
				act.terminate();
			}

		} else

		if (type.equals(FName.wk_object_event_type_wkitem)) {
	
			if (event.equals(FName.wk_item_event_to_create)) {
			
				if(wkctx.getActInstFrom() > 0){
					WorkItem item = new WorkItemNormal(wkctx,wkctx.getActInstFrom(),wkDBid);		
					item.create();
				}else{
					WorkItem item = new WorkItemNormal(wkctx,wkDBid);		
					item.create();
				}
				
			} else {
     			WorkItem item = WorkItemFactory.getWorkItem(wkctx);			

				if (event.equals(FName.wk_item_event_to_complete)) {
					item.complete();
				}
				if (event.equals(FName.wk_item_event_to_receive)) {
					item.receive();
				}
				if (event.equals(FName.wk_item_event_to_reject)) {
					item.reject();
				}
				if (event.equals(FName.wk_item_event_to_resume)) {
					item.resume();
				}
				if (event.equals(FName.wk_item_event_to_run)) {
					item.run();
				}
				if (event.equals(FName.wk_item_event_to_suspend)) {
					item.suspend();
				}
				if (event.equals(FName.wk_item_event_to_terminate)) {
					item.terminate();
				}
			}
		}
	}

	
	protected void fireObserver(EngineContext wkctx) {
		this.wkctx = wkctx;
		String event = wkctx.getEvent();	
		if (event.equals(FName.wk_activity_event_to_notstart)
				|| (event.equals(FName.wk_activity_event_to_run))
				|| (event.equals(FName.wk_activity_event_to_complete))
				|| (event.equals(FName.wk_activity_event_to_resume))
				|| (event.equals(FName.wk_activity_event_to_abort))
				|| (event.equals(FName.wk_activity_event_to_suspend))
				|| (event.equals(FName.wk_activity_event_to_terminate))) {

			wkctx.setEventType(FName.wk_object_event_type_activity);
			
		} else	if (event.equals(FName.wk_item_event_to_complete)
				|| (event.equals(FName.wk_item_event_to_create))
				|| (event.equals(FName.wk_item_event_to_receive))
				|| (event.equals(FName.wk_item_event_to_reject))
				|| (event.equals(FName.wk_item_event_to_resume))
				|| (event.equals(FName.wk_item_event_to_run))
				|| (event.equals(FName.wk_item_event_to_suspend))
				|| (event.equals(FName.wk_item_event_to_terminate))) {

			wkctx.setEventType(FName.wk_object_event_type_wkitem);
			
		} else	if (event.equals(FName.wk_process_event_to_notstart)
				|| (event.equals(FName.wk_process_event_to_run))
				|| (event.equals(FName.wk_process_event_to_complete))
				|| (event.equals(FName.wk_process_event_to_cancel))
				|| (event.equals(FName.wk_process_event_to_resume))
				|| (event.equals(FName.wk_process_event_to_abort))
				|| (event.equals(FName.wk_process_event_to_suspend))
				|| (event.equals(FName.wk_process_event_to_terminate))) {

			wkctx.setEventType(FName.wk_object_event_type_process);
		}
		
		this.update(wkctx);
	}	
	
	
	protected void fireWorkflowEvent(EngineContext wkctx,int runWkDBid,String event) {
		this.wkctx = wkctx;
		this.wkctx.setWkDBid(runWkDBid);
		this.wkctx.setEvent(event);
		fireObserver(wkctx);
	}

}
